using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ViewOnCore.Helper.WPF
{

    public partial class IntellisenseTagsDialog : System.Windows.Controls.UserControl
    {

        #region Variables

        private IntellisenseTextBox _textBox;

        #endregion

        #region Constructor

        public IntellisenseTagsDialog(IntellisenseTextBox textBox)
        {
            InitializeComponent();

            _textBox = textBox;

            btnOk.Click += new RoutedEventHandler(btnOk_Click);
            btnCancel.Click += new RoutedEventHandler(btnCancel_Click);

            Initialize();
        }

        private void Initialize()
        {
            Dictionary<string, bool> list = new Dictionary<string, bool>();

            //---- Add the current content
            string[] currentTags = _textBox.Text.Trim().Split(new char[] { ';', ',' });
            foreach (string tag in currentTags)
                if (!list.ContainsKey(tag.Trim()))
                    list.Add(tag.Trim(), true);

            //---- Add all the keys
            foreach (string tag in _textBox.IntellisenseManager.Databases[_textBox.DatabaseIndex].Keys)
                if (!list.ContainsKey(tag))
                    list.Add(tag, false);

            //---- Add the entries
            List<string> sortedList = new List<string>(list.Keys);
            sortedList.Sort();
            foreach (string tag in sortedList)
            {
                if (tag.Length < 1)
                    continue;

                ListBoxItem item = new ListBoxItem();
                CheckBox checkBox = new CheckBox();
                checkBox.Content = tag;
                item.Content = checkBox;

                if (list[tag])
                    checkBox.IsChecked = true;

                lstChoices.Items.Add(item);
            }
        }

        #endregion

        #region Ok / Cancel

        void btnOk_Click(object sender, RoutedEventArgs e)
        {
            childWindowDecorator.Close();

            //---- Reconstruct the content
            string result = "";
            foreach (ListBoxItem item in lstChoices.Items)
            {
                CheckBox box = item.Content as CheckBox;
                if (box.IsChecked == true)
                {
                    string tag = box.Content as string;
                    if (result.Length > 0)
                        result += ";";
                    result += tag;
                }
            }

            _textBox.UpdateTextFromDialog(result);
        }

        void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            childWindowDecorator.Close();
        }

        #endregion

        #region ShowDialog

        /// <summary>
        /// Show the dialog.
        /// </summary>
        /// <param name="container">The desktop</param>
        public void ShowDialog(Desktop container)
        {
            childWindowDecorator.ShowDialog(container);
        }

        #endregion

    }
}